/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制UI配置表的窗体
* CreateTime：2020-07-07 14:56:32
* Version：1.0
* Modify Recorder：
*************************************************/

using System.IO;
using TinyToolKit;
using UnityEditor;
using TinyToolKit.UI;
using UnityEditorInternal;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class DrawWindowUITable : DrawWindowBase    
    {
        private bool isFoldoutCurForms = true;
        private bool isFoldoutStack = true;
        private TUIManager _uiManager;

        private SerializedObject _serializedObject;
        private ReorderableList _uiFormPrefabs;
        private TinyToolEditorSettingAsset _toolSetting;

        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolEditorSettingAsset)setting;
            _serializedObject = new SerializedObject(setting);
            
            _uiFormPrefabs = TinyToolWindowUtil.GetReorderableList(_serializedObject, "UIFormPrefabs", "窗口预制体","窗口");    
        }

        protected override void DrawInternalMainGUI()
        {
            _serializedObject.Update();

            _uiFormPrefabs.DoLayoutList();

            _toolSetting.uiFormConfigName = TinyToolWindowUtil.DrawTextField("UI配置表名：", _toolSetting.uiFormConfigName);
            EditorGUILayout.BeginHorizontal();
            _toolSetting.uiFormSavePath = EditorGUILayout.TextField("存储路径：", _toolSetting.uiFormSavePath);
            if (GUILayout.Button(EditorGUIUtility.FindTexture("Folder Icon"), GUILayout.MaxWidth(40), GUILayout.MaxHeight(20)))
            {
                string savePath = EditorUtility.OpenFolderPanel("UI配置表存储路径", "Assets", "Resources");
                if (savePath.IsNullOrEmpty() == false)
                {
                    int index = savePath.IndexOf("Assets");
                    savePath = savePath.Remove(0, index);
                    _toolSetting.uiFormSavePath = savePath;
                } 
            }
            EditorGUILayout.EndHorizontal();

            if (TinyToolWindowUtil.DrawButton("创建UI配置表"))
            {
                if (_toolSetting.uiFormSavePath.IsNullOrEmpty() == false)
                {
                    if (Directory.Exists(_toolSetting.uiFormSavePath) == false)
                        TinyToolWindowUtil.DrawWarnningDialog("存储路径不是有效路径！");
                    else
                        CreateConfigTable(_toolSetting.uiFormSavePath);
                }
                else
                {
                    TinyToolWindowUtil.DrawWarnningDialog("存储路径不能为空！");
                }
            }
            
            _serializedObject.ApplyModifiedProperties();
        }
        
        /// <summary>
        /// 创建窗体路径的配置表
        /// </summary>
        /// <param name="savePath">保存的路径</param>
        private void CreateConfigTable(string savePath)
        {
            UIFormPath formPath = GetFormPaths();

            if (formPath.UIFormPaths != null && formPath.UIFormPaths.Count > 0)
            {
                string targetPath = Path.Combine(savePath, _toolSetting.uiFormConfigName + ".json");
                if (File.Exists(targetPath))
                    File.Delete(targetPath);
                File.Create(targetPath).Dispose();

                string json = JsonUtility.ToJson(formPath);
                File.WriteAllText(targetPath, json);

                EditorUtility.DisplayDialog("创建UI窗体配置表", "UI窗体配置表 " + _toolSetting.uiFormConfigName + ".json 保存在" + savePath, "确定");
            }
            else
            {
                EditorUtility.DisplayDialog("创建UI窗体配置表", "请先添加UI窗体的预制体到列表中", "确定");
            }
        }
        
        //获取所有UI窗体预制体的路径
        private UIFormPath GetFormPaths()
        {
            UIFormPath formPath = new UIFormPath();
            UIFormBase[] UIFormPrefabs = _toolSetting.UIFormPrefabs;
            for (int i = 0; i < UIFormPrefabs.Length; i++)
            {
                string path = AssetDatabase.GetAssetPath(UIFormPrefabs[i].gameObject);
                string name = Path.GetFileNameWithoutExtension(path);
                formPath.Add(name, path);
            }
            return formPath;
        }
    }
}
